package com.gugee.tiktok.common.model.dal.music;


import com.gugee.tiktok.common.model.mysql.Music;
import com.gugee.tiktok.data.common.msg.MusicMessage;
import org.springframework.data.jdbc.repository.query.Query;
import org.springframework.data.repository.CrudRepository;

import java.util.*;

public interface MusicRepository extends CrudRepository<Music,Long>,MusicExtRepository {

    Optional<Music> findByMid(long mid);
    List<Music> findByMidIn(List<Long> mids);


    @Query("select b.o_region as region,count(*) as count " +
            "from music m " +
            "left join aweme a on m.mid=a.o_music_id " +
            "left join blogger_main b on a.o_uid = b.o_uid " +
            "where m.mid = :mid " +
            "group by b.o_region order by count desc limit 0,1")
    Map<String,Object> getBelongRegion(long mid);


    @Query("select b.blogger_type as type,count(*) as count " +
            "from music m " +
            "left join aweme a on m.mid=a.o_music_id " +
            "left join blogger_main b on a.o_uid = b.o_uid " +
            "where m.mid = :mid " +
            "group by b.blogger_type order by count desc")
    List<Map<String,Object>> getBelongType(long mid);

    @Query("SELECT id,  mid  FROM music  order by id  limit :start,:records")
    List<MusicMessage> getMusicsByStartTimeOrdOrderBy(long start, long records);

    @Query("SELECT   o_music_id as mid  FROM aweme order by score desc LIMIT  :start,:records")
    List<MusicMessage> getMusicsTopOrderByScore(long start, long records);

    @Query("SELECT mid FROM music ORDER BY user_growth DESC LIMIT :start,:records")
    Set<Long> getMusicsTopOrderByUserGrowth(long start, long records);
}
